package com.haiyi.software.app;

import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.JsonObject;
import com.haiyi.software.app.basic.ActivityBasic;
import com.haiyi.software.app.basic.ActivityMySayBasic;
import com.haiyi.software.tools.L;

public class ActivityMySay extends ActivityMySayBasic implements
		OnClickListener {

	private EditText Editx_say;

	private TextView TextView_num;

	private Button btn_save;

	private int mNum = 50;

	private String str_sign;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO 自动生成的方法存根
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_mysay);

		initParam();
		initCom();
		initData();
	}

	private void initParam() {

		str_sign = getIntent().getStringExtra("sign").toString();

		if (str_sign.equals("未填写")) {

			str_sign = "";
		}
	}

	private void initData() {

		Editx_say.setText(str_sign);
		TextView_num.setText(String.valueOf(mNum) + "/50");

	}

	private void initCom() {

		this.Editx_say = (EditText) this.findViewById(R.id.Editx_say);
		this.TextView_num = (TextView) this.findViewById(R.id.TextView_num);
		this.btn_save = (Button) this.findViewById(R.id.btn_save);

		this.Editx_say.addTextChangedListener(watcher);
		this.btn_save.setOnClickListener(this);
	}

	private TextWatcher watcher = new TextWatcher() {

		private CharSequence mTemp;

		private int mSelectionStart;

		private int mSelectionEnd;

		@Override
		public void onTextChanged(CharSequence s, int start, int before,
				int count) {
			// TODO 自动生成的方法存根
			mTemp = s;
		}

		@Override
		public void beforeTextChanged(CharSequence s, int start, int count,
				int after) {
			// TODO 自动生成的方法存根

		}

		@Override
		public void afterTextChanged(Editable editable) {
			// TODO 自动生成的方法存根
			int number = mNum - editable.length();
			TextView_num.setText(String.valueOf(number) + "/50");
			mSelectionStart = Editx_say.getSelectionStart();
			mSelectionEnd = Editx_say.getSelectionEnd();
			L.i("dsa", "" + mSelectionStart + " " + mSelectionEnd);
			if (mTemp.length() > mNum) {
				editable.delete(mSelectionStart - 1, mSelectionEnd);// 删掉多输入的文字
				int tempSelection = mSelectionEnd;
				Editx_say.setText(editable);
				Editx_say.setSelection(tempSelection);
			}
		}
	};

	@Override
	protected void requestFinish(JsonObject jsonObjectReturn) {

		if (jsonObjectReturn.get("state").getAsString().equals("200")) {

			Toast.makeText(this, jsonObjectReturn.get("desc").getAsString(),
					Toast.LENGTH_LONG).show();

			MainApplication.getInstance().getMineUserInfo().setSign(str_sign);

			setResult(ActivityMine.UPDATE_USER_SIGN_SUCCESS);

			this.finish();

		} else {
			Toast.makeText(this, "保存签名失败", Toast.LENGTH_LONG).show();
		}
	}

	@Override
	protected void requestError() {

		Toast.makeText(this, "网络连接错误", Toast.LENGTH_LONG).show();
	}

	@Override
	public void onClick(View v) {

		switch (v.getId()) {

		case R.id.btn_save:

			str_sign = Editx_say.getText().toString().trim();

			updUserSign(str_sign);

			break;
		}
	}
}
